<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';


//Người tạo : Hoàng Hải Luân - Team 4
//Module 	: Menu
//Chức năng	: Show menu
 
class Menu_IndexController {

	public function init(){
		parent::init();
	}

	public function indexAction(){
		$tbMenu = new Menu_Model_Menu();
		$tbLanguage = new Language_Model_Language();
		$Menus = "";
		//get language
		$lang = 0;
		if(isset($_SESSION['language']))
			$lang = $_SESSION['language'];
		//Get value of menu
		$Menus = $this->findMenuItem($tbMenu, 0,$lang);
		if($Menus == ""){
			$allMenu = null;
		}else{
			$allMenu = $Menus;
		}

		$this->view->allMenu	= $allMenu;
	}

	// ham de quy tim menu con
	public function findMenuItem($table,$parent_id,$lang){
		$cate = "";
		$data = $table->selectMenuItemByParentId($parent_id,$lang);
		if($parent_id == 0){
			if(count($data) == 0)
				$data = $table->selectMenuItemByParentId($parent_id,0);
		}
		if(count($data) > 0){
			if($parent_id != 0)
				$cate .= '<ul style="visibility: hidden; display: block;">';
				
			foreach ($data as $d){
				$cate .= '<li><a href="'.$d['link'].'">'.$d['title'].'</a>';
				$cate .= $this->findMenuItem($table,$d['menu_id'],$lang);
				$cate .= '</li>';
			}
			if($parent_id != 0)
				$cate .= '</ul>';
		}
		return $cate;
	}
	

}